#include "rtsp.hpp"

#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int rtspplay()
{
    //VideoCapture videoCapture("rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov");
    VideoCapture videoCapture("rtsp://192.168.1.10:554/user=admin&password=&channel=1&stream=0.sdp?");
    videoCapture.set(CAP_PROP_FORMAT,-1);
    Size size = Size(videoCapture.get(CAP_PROP_FRAME_WIDTH), videoCapture.get(CAP_PROP_FRAME_HEIGHT));
    if (size.width <= 0 || size.height <= 0)
    {
        cout << "ERROR while get the width or the height" << endl;
    }
    else {
        cout << "width: " << size.width << "\nheight: " << size.height << endl;
    }
//    int myFourCC = VideoWriter::fourcc('X', 'V', 'I', 'D');//avi
//    //int myFourCC = VideoWriter::fourcc('m', 'p', '4', 'v');//mp4
//    //'m', 'p', '4', 'v'//'F','L','V','1'//'m', 'p', '4','2' //'m','p','4','v'//'d','i','v','x'
    double rate = videoCapture.get(CAP_PROP_FPS);
    cout<<"fps:"<<rate<<endl;
//    VideoWriter writer("/home/oceanstar/CLionProjects/hello.avi", myFourCC, rate, size, true);
    if (!videoCapture.isOpened()) {
        cout << "Video not open!" << endl;
        return 1;
    }

    Mat frame;

    int i = 10;
    while (i){
            bool ret = videoCapture.read(frame);
            if (ret){
                cout<<frame.cols<<"--"<<frame.rows<<endl;
                if(i==1)
                {
                    FILE *fp = NULL;
                    int ret;
                    std::string file = "test.dat";
                    fp = fopen(file.c_str(), "wb+");
                    fwrite(frame.data,1,frame.cols,fp);
                    fclose(fp);
                }
                i--;
            }
            waitKey(1);
    }

    cout << "FINISH ALL WORK----------" << endl;
    videoCapture.release();
//    writer.release();
//    destroyAllWindows();
    system("pause");
    return 0;
}

rtsp::rtsp()
{

}

